//**************************************//
// DXFreader for USING DXFLIB 12082011  //
//    Riku Maikola rpm@ac-mapa.org      //
//**************************************//
#ifndef __image__
#define __image__

#include <vector>
#include <utility>      // std::swap
#include <algorithm>
#include <memory>
#include <stdlib.h>
#include <wx/string.h>

#include "vertex.h"

#include "mPoint.h"
#include "mMath.h"
#include "mArea.h"
#include "mElements.h"
#include "mSolver.h"
#include <wx/tokenzr.h>

class element;
class eLine;
class eArc;
class eCircle;
class eRay;
class eTriangle;
class mChain;

#define m_Empty 0
#define m_Image 1
#define m_Chain 2
#define m_Mesh 3

//*************************************//
class Image : public mArea
{
private:
    uint type = 0;
    bool updated=false;
public:
    std::vector<element *> ME;   //************

    //***************************//
    wxString FileName = _("");
    wxString info = _("");

    Image();
    ~Image()
    {
        Clear();
    };

    //**************************************************************************************
    bool add(eLine);
    bool add(eArc);
    bool add(eCircle);
    bool add(eTriangle);
    bool Box(mPoint,mPoint);

    //***************************************************************************************
    void SetType(uint);
    uint IsType();

    //***************************************************************************************
    bool Copy(Image *,mPoint P = mP(0,0,0));               //paste this image to another image
    bool CopySelected(Image *,mPoint P = mP(0,0,0));       //paste this image to another image
    bool Del();                                            //DELETE SELECTED...
    bool Clear();                                          //Clear all

    //SELECT
    bool Select(uint);                                     //set flag to 1 or 0
    bool SelectALL();                                      //set flag to 1 or 0
    bool SelectConnected();
    bool SelectArea(mPoint,mPoint);

    //deselect
    bool DeselectAll();

    //modify
    bool Swap( uint , uint );

    //*****************//
    bool Rotate(double);
    void MirrorX();
    bool Move(mPoint);

    //Move from reletive selected elements
    bool MoveRight();
    bool MoveLeft();
    bool MoveDown();
    bool MoveUp();
    bool MoveVerticalCenter();
    bool MoveHorizontalCenter();

    bool MoveSelected(mPoint);

    //**********************************//
    mPointArray Ray(int,mPoint,mPoint);          //BLAST A RAY AND GET HIT POINTS from SINGLE element
    mPointArray IntImgRay(mPoint,mPoint);        //intersect img with Ray.........
    double Distance(mPoint);

    //VBO OBJECT MANUFACTION
    bool Lathe(DrawBuffer * DB);                                //lathe image to mesh..
    bool Refresh(DrawBuffer * DB);                              //

    //********************************
    //** CHAIN REPLACEMENT          **
    int T(int S);                    //G0,G1,G2,G3
    mPoint P1(int S);
    mPoint P2(int S);
    mPoint PC(int S);

    double R(int S);                 //
    double A1(int S);                //
    double A2(int S);                //

    uint Size();
    wxString CTG(double r,double z,bool T=false);

    bool MakeChain();
    bool Flip();
    bool Offset(double,Image * W);

    //OFFSETS**************************************************************************
    bool LineToLineOffset(mPoint *p,mPoint p1,mPoint p2,mPoint p3,double o);
    bool LineToArcOffset(mPoint *p,mPoint p1,mPoint p2,mPoint p3,long,double o);
    bool ArcToArcOffset(mPoint *p,mPoint cp1,mPoint p1, mPoint cp2,double R1,double R2);
};

//******************************************************************
bool isPointInsideTriangle(mPoint v0,mPoint v1,mPoint v2,mPoint pt);
double GetToolOffset(double,Image * tool,Image * Z_Image);
#endif
